/**
 * 
 */
package org.hsqldb.jdbclib;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author flashwei@gmail.com (wei)
 * 
 */
public class JdbcConnectionPool {

	private static JdbcConnectionPool pool = new JdbcConnectionPool();

	public static JdbcConnectionPool getInstance() {
		return pool;
	}

	public JdbcConnectionPool() {
	}

	public Connection getConnection(String driverClass, String jdbcURL,
			String user, String pass) {
		Connection connection = null;
		Properties info = new Properties();

		info.setProperty("proxool.maximum-connection-count", "5");
		info.setProperty("proxool.minimum-connection-count", "0");
		info.setProperty("proxool.house-keeping-test-sql",
				"select CURRENT_DATE");
		info.setProperty("proxool.house-keeping-sleep-time", "300");
		info.setProperty("user", user);
		info.setProperty("password", pass);

		String alias = String.valueOf((jdbcURL + user).hashCode());
		String url = "proxool." + alias + ":" + driverClass + ":" + jdbcURL;
		try {
			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
			connection = DriverManager.getConnection(url, info);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	
	public static void close(Connection connection) {
		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
